﻿using System.ComponentModel;
using System.Xml.Serialization;
using System.Collections.Generic;
using System;

namespace OpenWings.Entities
{
    [Serializable]
    [XmlRoot("servers", Namespace = "", IsNullable = false)]
    public class ServerConfig : Entity
    {
        EntityList<Group> _Groups;
        
        public EntityList<Group> Groups
        {
            get
            {
                if (_Groups == null)
                {
                    _Groups = new EntityList<Group>();
                    _Groups.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(Groups_DirtyChanged);
                }

                return _Groups;
            }
        }

        void Groups_DirtyChanged(object sender, DirtyChangedEventArgs e)
        {
            IsDirty = e.Dirty;
        }

        [XmlElement("group")]
        public Group[] szGroups
        {
            get
            {
                return Groups.ToArray();
            }
            set
            {
                if (value != null)
                {
                    _Groups = new EntityList<Group>((IEnumerable<Group>) value);
                    _Groups.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(Groups_DirtyChanged);
                }
            }
        }

        public override void OnDirtyTrackingChanged(object sender, DirtyTrackingChangedEventArgs e)
        {
            _Groups.DirtyTracking = e.Enabled;
            base.OnDirtyTrackingChanged(sender, e);
        }

        public override void Clean()
        {
            _Groups.Clean();

            base.Clean();
        }
    }

    [Serializable]
    public class Group : Entity
    {
        EntityList<Group> _Groups;
        EntityList<Server> _Servers;

        string _Name;

        public EntityList<Group> Groups
        {
            get
            {
                if (_Groups == null)
                {
                    _Groups = new EntityList<Group>();
                    _Groups.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(GroupsOrServers_DirtyChanged);
                }

                return _Groups;
            }
        }

        void GroupsOrServers_DirtyChanged(object sender, DirtyChangedEventArgs e)
        {
            IsDirty = e.Dirty;
        }

        [XmlElement("group")]
        public Group[] szGroups
        {
            get
            {
                return Groups.ToArray();
            }
            set
            {
                if (value != null)
                {
                    _Groups = new EntityList<Group>((IEnumerable<Group>) value);
                    _Groups.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(GroupsOrServers_DirtyChanged);
                }
            }
        }

        public EntityList<Server> Servers
        {
            get
            {
                if (_Servers == null)
                {
                    _Servers = new EntityList<Server>();
                    _Servers.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(GroupsOrServers_DirtyChanged);
                }

                return _Servers;
            }
        }

        [XmlElement("server")]
        public Server[] szServers
        {
            get
            {
                return Servers.ToArray();
            }
            set
            {
                if (value != null)
                {
                    _Servers = new EntityList<Server>((IEnumerable<Server>) value);
                    _Servers.DirtyChanged += new EventHandler<DirtyChangedEventArgs>(GroupsOrServers_DirtyChanged);
                }
                   
            }
        }

        [XmlAttribute("name")]
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                IsDirty = true;
            }
        }

        public override void OnDirtyTrackingChanged(object sender, DirtyTrackingChangedEventArgs e)
        {
            Groups.DirtyTracking = e.Enabled;
            Servers.DirtyTracking = e.Enabled;

            base.OnDirtyTrackingChanged(sender, e);
        }

        public override void Clean()
        {
            _Groups.Clean();
            _Servers.Clean();

            base.Clean();
        }
    }

    [Serializable]
    public class Server : Entity
    {
        string _Name;
        string _LoginURI;
        string _Notes;
        byte[] _Picture;
        ServerType _Type;
        string _WebSite;

        [XmlAttribute("name")]
        public string Name
        {
            get
            {
                return _Name;
            }
            set
            {
                _Name = value;
                IsDirty = true;
            }
        }

        [XmlAttribute("loginuri")]
        public string LoginURI
        {
            get
            {
                return _LoginURI;
            }
            set
            {
                _LoginURI = value;
                IsDirty = true;
            }
        }

        [XmlAttribute("notes")]
        public string Notes
        {
            get
            {
                return _Notes;
            }
            set
            {
                _Notes = value;
                IsDirty = true;
            }
        }

        [XmlAttribute("picture", DataType = "base64Binary")]
        public byte[] Picture
        {
            get
            {
                return _Picture;
            }
            set
            {
                _Picture = value;
                IsDirty = true;
            }
        }

        [XmlAttribute("website")]
        public string WebSite
        {
            get
            {
                return _WebSite;
            }
            set
            {
                _WebSite = value;
                IsDirty = true;
            }
        }

        [XmlAttribute("type")]
        [DefaultValue(ServerType.Unknown)]
        public ServerType Type
        {
            get
            {
                return _Type;
            }
            set
            {
                _Type = value;
                IsDirty = true;
            }
        }
    }

    [Serializable]
    public enum ServerType
    {
        Unknown,
        Standalone,
        Grid,
    }
}
